// ---------------------------------------------------------------------
//
// -- 数学工具库
// -- author £
//
// ---------------------------------------------------------------------

var MathUtil = {
    // //获取两点之间的距离
    // getDistance: function (x1, y1, x2, y2) {
    //     return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    // },

    //获取随机整数
    getIntRandom: function (start, end) {
        if (arguments.length === 1) {
            end = start;
            start = 0;
        }
        
        return start + Math.floor(Math.random() * (end - start + 1));
    },

    //获取地理距离
    getGeoDistance: function (lon1, lat1, lon2, lat2) {
        let EARTH_RADIUS = 6378137;
        let rad = cc.degreesToRadians,
            radLat1 = rad(lat1),
            radLat2 = rad(lat2),
            radLon1 = rad(lon1),  
            radLon2 = rad(lon2),
            x1, y1, z1, x2, y2, z2, d, theta;
  
        if (radLat1 < 0)  
            radLat1 = Math.PI / 2 + Math.abs(radLat1);// south  
        if (radLat1 > 0)  
            radLat1 = Math.PI / 2 - Math.abs(radLat1);// north  
        if (radLon1 < 0)  
            radLon1 = Math.PI * 2 - Math.abs(radLon1);// west  
        if (radLat2 < 0)  
            radLat2 = Math.PI / 2 + Math.abs(radLat2);// south  
        if (radLat2 > 0)  
            radLat2 = Math.PI / 2 - Math.abs(radLat2);// north  
        if (radLon2 < 0)  
            radLon2 = Math.PI * 2 - Math.abs(radLon2);// west  

        x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);  
        y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);  
        z1 = EARTH_RADIUS * Math.cos(radLat1);  
  
        x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);  
        y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);  
        z2 = EARTH_RADIUS * Math.cos(radLat2);  
  
        d = Math.sqrt((x1 - x2) * (x1 - x2) + 
            (y1 - y2) * (y1 - y2) + 
            (z1 - z2) * (z1 - z2));  
        //余弦定理求夹角  
        theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + 
            EARTH_RADIUS * EARTH_RADIUS - d * d) / 
            (2 * EARTH_RADIUS * EARTH_RADIUS));  
        return theta * EARTH_RADIUS;  
    },
};

module.exports = MathUtil;
